<template>
  <div class="my-tag">
    <input
      class="input"
      type="text"
      placeholder="输入标签"
      v-if="isEdit"
      @blur="isEdit = false"
      ref="ipt"
      :value="value"
      @keyup.enter="handleEnter"
    />
    <div class="text" v-else @dblclick="handleDb">{{ value }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isEdit: false,
    };
  },
  props: {
    value: {
      type: String,
    },
  },
  methods: {
    handleDb() {
      this.isEdit = true;
      this.$nextTick(() => {
        this.$refs.ipt.focus();
      });
    },
    handleEnter(e) {
      this.$emit("input", e.target.value);
      this.isEdit = false;
    },
  },
};
</script>

<style lang="less">
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>
